package com.esm.sm800.util;

import android.content.Context;
import android.os.Handler;
import android.widget.Toast;

import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.LinkedList;
import java.util.Queue;

import android_serialport_api.ComBean;
import android_serialport_api.SerialHelper;

//机台相关指令发送
/*
开仓门
回收针
出针
 */
public class NFCComm {

    private static final String TAG = NFCComm.class.getSimpleName();

    private SerialControl m_SerialPort;

    private static NFCComm jtcomm;

    private Context mContext;
    int m_nConnected = 0;

    public static void init(Context context) {
        if (jtcomm == null) {
            jtcomm = new NFCComm(context);
        }
    }

    public static NFCComm getInstance() {
        return jtcomm;
    }

    public NFCComm(Context context) {
        mContext = context;

        String szDevice = "/dev/ttyS1";
        int baudrate = 115200;

        m_SerialPort = new SerialControl();
        m_SerialPort.setPort(szDevice);
        m_SerialPort.setBaudRate(baudrate);
        try {
            m_SerialPort.open();
        } catch (SecurityException e) {
            Toast.makeText(mContext, "Open ttyUART device failed!", Toast.LENGTH_SHORT).show();
            return;
        } catch (IOException e) {
            Toast.makeText(mContext, "Open ttyUART device failed!", Toast.LENGTH_SHORT).show();
            return;
        } catch (InvalidParameterException e) {
            Toast.makeText(mContext, "Open ttyUART device failed!", Toast.LENGTH_SHORT).show();
            return;
        }
        m_nConnected = 1;

        DispQueue = new DispQueueThread();
        DispQueue.start();
    }

    public boolean CloseComm() {
        if (m_nConnected == 1) {  // tty uart mode
            m_SerialPort.stopSend();
            m_SerialPort.close();
        }
        m_nConnected = 0;
        return true;
    }

    //发送指令
    public boolean sendCommand() {
        if (m_nConnected == 0) {
            return false;
        }
        byte[] bytes = {0x01, 0x02, 0x03, 0x04};
        m_SerialPort.send(bytes, bytes.length);
        return true;
    }

    // uart control classes ======================================================
    private class SerialControl extends SerialHelper {

        public SerialControl() {
        }

        @Override
        protected void onDataReceived(final ComBean ComRecData) {
            //读到的串口数据
            DispQueue.AddQueue(ComRecData);
        }
    }

    private DispQueueThread DispQueue;

    private class DispQueueThread extends Thread {
        private Queue<ComBean> QueueList = new LinkedList<ComBean>();

        @Override
        public void run() {
            super.run();
            while (!isInterrupted()) {
                final ComBean ComData;
                while ((ComData = QueueList.poll()) != null) {
                    mComRecData = ComData;
                    if (mHandler != null) {
                        mHandler.sendEmptyMessage(RECEIVED_MSG);
                    }
                     try {
                        Thread.sleep(100);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }

        public synchronized void AddQueue(ComBean ComData) {
            QueueList.add(ComData);
        }
    }

    public Handler mHandler = null;
    public ComBean mComRecData = null;

    public static final int RECEIVED_MSG = 2666; //获取到串口信息


}
